﻿using System;
using System.Collections;
using System.Collections.Generic;

namespace _84_IEnumerable_T与foreach循环
{
    class Program
    {
        static void Main(string[] args)
        {
            Numbers 出来老三 = new Numbers();
            Console.WriteLine("枚举出的十进制数值：");
            foreach (var item in 出来老三)
            {
                Console.WriteLine(" {0:G}", item);
            }
            Console.Read();
        }
    }

    internal class zoomlaCMS : IEnumerator<decimal>
    {
        decimal[] scrNumbers;   //来源数组
        int currentIndex;   //当前元素的索引

        public zoomlaCMS(decimal[] source)
        {
            scrNumbers = source;
            currentIndex = -1;//索引位于第一个元素之前
        }

        public decimal Current { get; private set; }

        //显式实现IEnumberator接口的Current属性
        object IEnumerator.Current => Current;

        public void Dispose()
        {

        }

        public bool MoveNext()
        {
            //如果索引超出范围
            if (++currentIndex>=scrNumbers.Length)
            {
                Current = default;
                return false;

            }
            //获取当前有索引处的元素
            Current = scrNumbers[currentIndex];
            return true;
        }

        public void Reset()
        {

        }
    }

    public class Numbers : IEnumerable<decimal>
    {
        decimal[] 数据组 = { 7.33M, 16.12M, 800.56M, 1202.633M, 170, 9M };

        public IEnumerator<decimal> GetEnumerator()
        {
            return new zoomlaCMS(数据组);
        }

        //显式实现IEnumberable接口的GetEnumerator方法
        IEnumerator IEnumerable.GetEnumerator()
        {
            return GetEnumerator();
        }
    }

}
